package ru.pashkan.perspectives;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import ru.pashkan.db.dao.SitesDao;
import ru.pashkan.entity.Site;


@Component
public class NewSiteViewer {
    Shell shellNewSite;

    Text textSiteUrl = null;
    Text textSiteName = null;

    public NewSiteViewer() {
    }

    //    public NewSiteViewer() {
      public NewSiteViewer(Shell shell) {
        System.out.println("--------");
//       SitesListViewer sitesListViewer = new SitesListViewer();
//        Shell shell = sitesListViewer.createShell();

//        Display display = SitesListViewer.display;
//        Display display = ;
//        Shell shell = new Shell(display);
//        Shell shellNewSite = new Shell(shell, SWT.SHELL_TRIM);  //
         shellNewSite = new Shell(shell, SWT.DIALOG_TRIM);  //
          //TODO при закрытии дочернего шелла сделать активным родительский
//          shell.setEnabled(false);        //сделать родительский шелл неактивным


          setShellSettings(shellNewSite);
          getNewSiteItems(shellNewSite);

        shellNewSite.open();

    }

    private void setShellSettings(Shell shell){
        shell.setText("Добавить");
        shell.setBounds(150, 150, 700, 550);

        GridLayout gridLayout = new GridLayout(1, false);   //        gridLayout.numColumns = 1;
//
        shell.setLayout(gridLayout);
//        Composite parent = shell;
//
//        GridLayout gl_composite = new GridLayout(1, false);
//        gl_composite.horizontalSpacing = 0;
//        gl_composite.marginHeight = 0;
//        gl_composite.verticalSpacing = 0;
//        gl_composite.marginWidth = 0;
//        parent.setLayout(gl_composite);
    }

    private void getNewSiteItems(Shell shell){
        Link linkSiteName;
        Link linkEndNum;
        Group newSiteMainPanel;
        //Панель
        newSiteMainPanel = new Group(shell, SWT.None);

//        newSiteMainPanel.setSize(500, 300);

        GridLayout gridLayout = new GridLayout(4, true);
//        gridLayout.numColumns = 2;
//        gridLayout.horizontalSpacing = 4;
        gridLayout.marginHeight = 5;
//        gridLayout.verticalSpacing = 40;
        gridLayout.marginWidth = 5;
        newSiteMainPanel.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
//        gridData.heightHint = 100;
//        gridData.widthHint = 100;

        newSiteMainPanel.setLayoutData(gridData);
        newSiteMainPanel.setText("Основные параметры");
//        newSiteMainPanel.setVisible(true);

        //Название сайта
        linkSiteName = new Link(newSiteMainPanel, SWT.SHADOW_NONE);
        linkSiteName.setText("Название сайта");
//        linkStartNum.setToolTipText("Номер начального опыта");
//

//        textSiteName.setText("1");
//        textStartNum.setToolTipText("Номер начального опыта");
        gridData = new GridData(GridData.FILL, GridData.FILL, false, false);
        gridData.widthHint = 140;
        gridData.heightHint = 14;

        this.textSiteName = new Text(newSiteMainPanel, SWT.SINGLE | SWT.BORDER | SWT.LEFT);
        this.textSiteName.setText("New site");
        this.textSiteName.setLayoutData(gridData);



//        spStartNum = new Spinner (changeExpPanel, SWT.SINGLE | SWT.BORDER | SWT.RIGHT);
//        spStartNum.setMinimum(1);
//        spStartNum.setMaximum(1000);
//        spStartNum.setSelection(1);
//        spStartNum.setIncrement(1);
//        spStartNum.setPageIncrement(100);
//        spStartNum.setLayoutData(gridData);


        //spinner.pack();


        //Конечная ячейка
        Link linkSiteUrl = new Link(newSiteMainPanel, SWT.SHADOW_NONE);
        linkSiteUrl.setText("URL");
//        linkEndNum.setToolTipText("Номер конечного опыта");
//        linkEndNum.setBounds(130, 20, 50, 20);

		/*textEndNum = new Text(changeExpPanel, SWT.SINGLE | SWT.BORDER | SWT.RIGHT);
		textEndNum.setText("2");
		textEndNum.setSize(100, 10);
		textEndNum.setToolTipText("Номер конечного опыта");
		textEndNum.setBounds(180, 17, 50, 20);
		textEndNum.setLayoutData(gridData);*/

        this.textSiteUrl = new Text(newSiteMainPanel, SWT.SINGLE | SWT.BORDER | SWT.LEFT);
        this.textSiteUrl.setText("Site Url");
        this.textSiteUrl.setLayoutData(gridData);


        Button butSaveSite = new Button(newSiteMainPanel, SWT.PUSH);
        butSaveSite.setText("Save");
        gridData = new GridData(GridData.FILL, GridData.FILL, false, false);
        gridData.widthHint = 25;
        gridData.heightHint = 25;
        butSaveSite.setLayoutData(gridData);
        butSaveSite.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                if(saveNewSite()){

                    closeShell();
                }
            }
        });

        Button butCancel = new Button(newSiteMainPanel, SWT.PUSH);
        butCancel.setText("Cancel");
        gridData = new GridData(GridData.FILL, GridData.FILL, false, false);
        gridData.widthHint = 25;
        gridData.heightHint = 25;
        butCancel.setLayoutData(gridData);
        butCancel.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {


                    closeShell();

            }
        });
//        butSaveSite.addSelectionListener(
//                new org.eclipse.swt.events.SelectionAdapter() {
//
//                });
//        spEndNum = new Spinner (changeExpPanel, SWT.SINGLE | SWT.BORDER | SWT.RIGHT);
//        spEndNum.setMinimum(2);
//        spEndNum.setMaximum(1000);
//        spEndNum.setSelection(2);
//        spEndNum.setIncrement(1);
//        spEndNum.setPageIncrement(100);
//        spEndNum.setLayoutData(gridData);
    }
    void closeShell(){
        this.shellNewSite.dispose();
    }
    boolean saveNewSite(){
        Site site = new Site();
        site.setSiteName(this.textSiteName.getText());
        site.setSiteUrl(this.textSiteUrl.getText());

        ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext-db.xml"});
        SitesDao siteDao = (SitesDao) appContext.getBean("SitesDao");
        siteDao.saveSite(site);

        return true;
    }

}
